# -*- coding: UTF-8 -*-

##########################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

#### Applet配置信息获取 Applet configuration information acquisition

from time import sleep
from flask import Blueprint, current_app
from flask import request
import utils.pgsql.pgactivity as activity
import utils.pgsql.pgadmin
import utils.pgsql.pgcase
import utils.pgsql.pgvlogrecord as record
import utils.pgsql.pgdevice as device
from utils.wraps import token, urlcost
import utils.message.response as response
import json
import utils.pgsql.pgpoint as point

applet_module = Blueprint("party_app", __name__, url_prefix="/api/v1/party/app")



@applet_module.route("/case", methods=["GET"])
@urlcost.cost_count_wraps
def case_all():
   
   
    tokenInfo = request.headers["Authorization"]
    if tokenInfo == current_app.config["APPLET_KEY"]:
        obj = utils.pgsql.pgcase.PgCase()
        return response.make_response_success(obj.all())
    else:
        return response.make_response_401()


@applet_module.route("/case/operator/<int:operatorid>", methods=["GET"])
@urlcost.cost_count_wraps
def case_channel(operatorid):
 
    tokenInfo = request.headers["Authorization"]
    if tokenInfo == current_app.config["APPLET_KEY"]:
        obj = utils.pgsql.pgcase.PgCase()
        return response.make_response_success(obj.allByOperator(operatorid))
    else:
        return response.make_response_401()


@applet_module.route("/case/<int:caseId>", methods=["GET"])
@urlcost.cost_count_wraps
def case_info(caseId):
  
    tokenInfo = request.headers["Authorization"]
    if tokenInfo == current_app.config["APPLET_KEY"]:
        obj = utils.pgsql.pgcase.PgCase()
        return response.make_response_success(obj.get(caseId))
    else:
        return response.make_response_401()


@applet_module.route("/mission/case/<int:caseid>", methods=["GET"])
@urlcost.cost_count_wraps
def mission_case_applet(caseid):
 
    tokenInfo = request.headers["Authorization"]
    if tokenInfo == current_app.config["APPLET_KEY"]:
        obj = activity.PgActivity()
        return response.make_response_success(obj.getTemplInfo(caseid))
    else:
        return response.make_response_401()


@applet_module.route("/case/<int:caseId>/point", methods=["GET"])
@urlcost.cost_count_wraps
def points(caseId):

    tokenInfo = request.headers["Authorization"]
    if tokenInfo == current_app.config["APPLET_KEY"]:
        obj = point.PgPoint()
        return response.make_response_success(obj.allByCase(caseId))
    else:
        return response.make_response_401()

@applet_module.route("/case/<int:caseId>/device", methods=["GET"])
@urlcost.cost_count_wraps
def devices(caseId):
  
    tokenInfo = request.headers["Authorization"]
    if tokenInfo == current_app.config["APPLET_KEY"]:
        obj = device.PgDevice()
        return response.make_response_success(obj.allByCase(caseId))
    else:
        return response.make_response_401()

@applet_module.route("/record", methods=["POST"])
@urlcost.cost_count_wraps
def record():
 
    tokenInfo = request.headers["Authorization"]
    if tokenInfo == current_app.config["APPLET_KEY"]:
        dic = json.loads(request.data)
        if dic["NoteType"] == "Buy":
            result = record.PgRecord().updateBuyNumPlus1(dic["ProjectId"])
            if result:
                return response.make_response_success_no_data()
            else:
                return response.make_response_1000()
        if dic["NoteType"] == "Share":
            result = record.PgRecord().updateShareNumPlus1(dic["ProjectId"])
            if result:
                return response.make_response_success_no_data()
            else:
                return response.make_response_1000()
